
local IsClient = game:GetService("RunService"):IsClient();
local M=nil
local FunctionList=nil
local ItemTool=_G.CommonOHOH.load.ItemTool();

if  IsClient then
     M = _G.ClientOHOH.class.new(script, _G.ClientOHOH.files.Client_PlayerComponent(),"mall_handle_base"..script.Name);
     FunctionList = _G.ClientOHOH.load.FunctionList();
else

     M = _G.ServerOHOH.class.new(script, _G.ServerOHOH.files.Server_PlayerComponent(),"mall_handle_base"..script.Name);
end

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);	
    self.player=parent
    self.player.CanNotRobuxBuyEvent:Connect(function(gid,gtype,...)
        if self:IsMe(gid,gtype) then
             if not  self:Check(gid) then
                    return true
             end
        end
    end)
    self.player.RobuxBuyDoneEvent:Connect(function(gid,gtype,...)
        if self:IsMe(gid,gtype) then
            if  IsClient then
                self:ClientBuyDone(gid,gtype,...)
            else
                self:ServerBuyDone(gid,gtype,...)
            end
        end
    end)

    if  IsClient then
        self.DataBase=self.player:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())

    else
        self.DataBase=self.player:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())
        self.ItemCtrl=self.player:GetComponent(_G.ServerOHOH.load.Server_ItemCtrl())
    end
end;

function M.dtor(self)
	M.super.dtor(self);
end;

--检查是否是自己，子类去覆盖
function M.IsMe(self,gid,gtype)
    return  "base"==gtype
end

--检查是否能够购买（子类去覆盖）(服务器，客户端都会调用)
function M.Check(self,gid,gtype)
    return true --待实现
end





--购买完成，客户端处理
function M.ClientBuyDone(self,gid,gtype,...)

end

--服务器验证购买完成，处理发放逻辑（子类去覆盖）
function M.ServerBuyDone(self,gid,gtype,...)
    local cfg=_G.CommonOHOH.config.Goods[gid]
    for k,v in pairs(cfg.itemData) do
        local key,count= ItemTool.GetRewardByLevel(k,v,self.DataBase:GetData("base").level)
        self.ItemCtrl:AddItem({[key]=count})
    end
    self.parent:SynchDataToClient("base")
end


return M